package com.hu.zhcc.shiro.service.HealthIntervention;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.dao.PopulationDiseaseDAO;
import com.hu.zhcc.shiro.dao.PopulationclassificationMapper;
import com.hu.zhcc.shiro.entity.bo.HealthIntervention.Disease;
import com.hu.zhcc.shiro.entity.bo.Member;
import com.hu.zhcc.shiro.entity.dto.HealthIntervention.PopulationDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PopulationclassificationServiceImpl implements PopulationclassificationService {
    @Autowired
    public PopulationclassificationMapper mapper;

    @Autowired
    public PopulationDiseaseDAO dao;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    //根据编号查询获得分类信息
    @Override
    public PopulationDTO selectByPrimaryKey(int id) {
        Member member = mapper.selectByPrimaryKey(id);
        if(member == null) {
            return null;
        }
        PopulationDTO dto = new PopulationDTO();
        BeanUtils.copyProperties(member,dto);
        // 得到人群分类
        List<Disease> popDis = dao.getPopDis(id);
        int[] dis = new int[popDis.size()];
        for (int i =0 ; i<dis.length ; i++){
            dis[i] = popDis.get(i).getDisId();
        }
        dto.setDisIds(dis);
        return dto;
    }
    //初始查询
    @Override
    public Page<Member> selectByPopList(Map map, int offset , int limit) {
        PageHelper.startPage(offset,limit);
        List<Member> population = mapper.selectByPopList(map);
        PageInfo<Member> info = new PageInfo<>(population);
        return new Page<>(info.getTotal(),population);
    }

    //变更分类
    @Override
    public int updateByPopulationDTO(PopulationDTO dto) {
        Member member = new Member();
        BeanUtils.copyProperties(dto,member);
        int up = mapper.updateByIdUPStart(member);
        if(up > 0) {
            int i = dao.removeById(dto.getId());
            if (dto.getDisIds() != null && dto.getDisIds().length > 0) {
                dao.savePopulationDisease(dto.getId(), dto.getDisIds());
            }
        }
        return up;
    }

    @Override
    public int updateByIdFormulateStart(Integer id) {
        return mapper.updateByIdFormulateStart(id);
    }

    @Override
    public Member findPopulation(Integer id) {
        return mapper.findPopulation(id);
    }
}
